import Vue from "vue"
import axios from "axios"
import BScroll from 'better-scroll'
import Header from '@/components/Header'
import Banner from '@/components/Banner'
import Nav from '@/components/Nav'
import Foodlists from '@/components/Foodlists'
import Foodlist from '@/components/Foodlist'
import Buycart from '@/components/Buycart'

	export default {
		name:"index",
		components:{
	    	'Headerm':Header,   	    	
	    	'Bannerm':Banner,   	    	
	    	'Navm':Nav ,  	    	
	    	'Foodlistsm':Foodlists ,  	    	
	    	'Foodlistm':Foodlist ,  	    	
	    	'Buycart':Buycart ,
	    	
	    	  	    	
	   },
	   data(){
	    	return{
	    		
	    		goorderzt:1,//下方确认订单状态
	    		ok:false,	    		
	    		nav1:true,	    		
	    		nav2:false,	    		
	    		nav3:false,	    		
	    		nav4:false,	    		
	    		bannerimgs:[
		    		{imgurl:'/static/images/banner1.jpg'},
		    		{imgurl:'/static/images/banner2.jpg'},
		    		{imgurl:'/static/images/banner3.png'},
	    		],
	    		foodfls: [],
				foods:[],			  	    		       		    
				dgfoods:[],			  	    		       		    
     		    cplxName:'',
     		    cplxId:0,
     		    cartfoods:[],
     		    totalprice:0,
    			numbers:0,
    			dindnavshow:true,
    			authUrl:"https://open.weixin.qq.com/connect/oauth2/authorize"
	+ "?appid=wx88f75727970dcb7d&redirect_uri=http%3a%2f%2fdc.benft.com%2fwxpay%2fgetOpenid"
	+ "&response_type=code&scope="
	+ "snsapi_base&state=123#wechat_redirect"
	    	}
	   },
	   created() {
		    axios.get('/api/dishesTypeList').then((res) => {
		      this.foodfls = res.data;		      
		      this.cplxName=this.foodfls[0].name;//默认显示第一个	     
		      this.cplxId=this.foodfls[0].id;//默认显示第一个
		      axios.get('/api/dishesListByCplxId?cplx_id='+this.cplxId).then((res) => {
			      this.foods = res.data;		      		     
			      console.log(this.foods)		      
		      });	
		    });		    		    		   	   
	   },
	   mounted(){ 
	   		
	   		console.log(this.goorderzt);
		   	axios.get('/api/yidiancaiping').then((res) => {	 
	  			//console.log(res.data);
					this.cartfoods=res.data;
					//console.log(this.cartfoods);
					this.numbers=0;
					this.goorderzt=sessionStorage["zt"];
					this.totalprice=0;					
					for (var i=0;i<this.cartfoods.length;i++){					
							var price=this.cartfoods[i].cpjg;
							var number=Number(this.cartfoods[i].ydfs);							
							this.totalprice +=price*number;
							this.numbers+=number;
						}
			});	
			
  	   },
	   methods: {
	    	foodflshow: function (){//菜品分类显示 
				this.ok=true;								
	    	},	
	    	foodflhide: function (){ 
				this.ok=false;
	    	},
	    	ddnavshow: function (cartlistshow){ //订单按钮显示
				this.dindnavshow=!cartlistshow;				
	    	},

	    	navname: function (index){	    		
	    		if(index===0){
	    			this.nav1=true;
	    			this.nav2=false;	    		
		    		this.nav3=false;    		
		    		this.nav4=false;	 
	    		}if(index===1){
	    			this.nav1=false;
	    			this.nav2=true;
	    			this.nav3=false;    		
		    		this.nav4=false;
		    		axios.get('/api/dishesList').then((res) => {
					      this.dgfoods = res.data;	     
					})
	    		}if(index===2){
	    			this.nav1=false;
	    			this.nav2=false;
	    			this.nav3=true;    		
		    		this.nav4=false;
	    		}if(index===3){
	    			this.nav1=false;
	    			this.nav2=false;
	    			this.nav3=false;    		
		    		this.nav4=true;
	    		}
	    	},
	    	flname: function (index, event,foodfl){ 	    		
	    		var flid=foodfl.id;
	    		 axios.get('/api/dishesListByCplxId?cplx_id='+flid).then((res) => {
				      this.foods = res.data;
				      this.cplxName=foodfl.name;
				      this.cplxId=foodfl.id;
				      console.log(this.foods)
				    })
	    		this.$nextTick(function () {//分类样式
		　　　　　　　　this.foodfls.forEach(function (foodfl) {
		　　　　　　　　　　Vue.set(foodfl,'active',false);
		　　　　　　　　});
		　　　　　　　　Vue.set(foodfl,'active',true);
	　　　　　　    });		    						
	        },
	        
	        addcart: function (cartfoods,totalprice,numbers,goorderzt){	
		  	    this.totalprice=totalprice;
		  	    this.numbers=numbers;
		  	    this.cartfoods=cartfoods;
		  	    this.goorderzt=goorderzt;
		  	    //console.log(this.cartfoods);
		  	  },
		  	//购物车数量加减
		  	subtract: function (cartfood){
		  		axios.get('/api/quxiaodiancai?cpid='+cartfood.id).then((res) => {
		  			this.cartfoods=res.data;
    				//console.log(this.cartfoods);
    				this.numbers=0;
    				this.totalprice=0;
    				for (var i=0;i<this.cartfoods.length;i++){					
							var price=this.cartfoods[i].cpjg;
							var number=Number(this.cartfoods[i].ydfs);
							console.log(i);
							console.log(number);
							this.totalprice +=price*number;
							this.numbers+=number;
						}		  			
		  		})		  				  		
			},
		  	add: function (cartfood){		  		
				axios.get('/api/diancai?cpid='+cartfood.id).then((r) => {	      		     			      			      
		      		axios.get('/api/yidiancaiping').then((res) => {	 		      			
	    				this.cartfoods=res.data;
	    				this.numbers=0;
	    				this.totalprice=0;
	    				for (var i=0;i<this.cartfoods.length;i++){					
							var price=this.cartfoods[i].cpjg;
							var number=Number(this.cartfoods[i].ydfs);
							console.log(i);
							console.log(number);
							this.totalprice +=price*number;
							this.numbers+=number;
						}
				    	this.$emit('addcart',this.cartfoods,this.totalprice,this.numbers);
	      			});	
			    });	
			   	
		  	},
	   }
	}   